< previous page page_876 next page >

Page 876
Listing A.4. continued
Private Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA"
(ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long,
ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
Note that you can place API declarations directly in a form as long as you precede these declaration lines with the Private keyword.
Next, create a new method called GetKeyValue to use these API calls and constants. Enter the code from Listing A.5 into the project.
Type.gifListing A.5. Coding the GetKeyValue function.
Public Function GetKeyValue(KeyRoot As Long, KeyName As String, SubKeyRef
As String, ByRef KeyVal As String) As Boolean
    Dim i As Long                                           ' Loop Counter
    Dim rc As Long                                          ' Return Code
    Dim hKey As Long                                        ' Handle To An Open
arrow.gifRegistry Key
    Dim hDepth As Long                                      '
    Dim KeyValType As Long                                  ' Data Type Of A
arrow.gifRegistry Key
    Dim tmpVal As String                                    ' Tempory Storage
arrow.gifFor A Registry Key Value
    Dim KeyValSize As Long                                  ' Size Of Registry
arrow.gifKey Variable
    '------------------------------------------------------------
    ' Open RegKey Under KeyRoot {HKEY_LOCAL_MACHINE...}
    '------------------------------------------------------------
    rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hKey) ' Open Registry
arrow.gifKey

    If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError          ' Handle Error...

    tmpVal = String$(1024, 0)                             ' Allocate Variable
arrow.gifSpace
    KeyValSize = 1024                                       ' Mark Variable Size

    '------------------------------------------------------------
    ' Retrieve Registry Key Value...
    '------------------------------------------------------------
    rc = RegQueryValueEx(hKey, SubKeyRef, 0, _
                         KeyValType, tmpVal, KeyValSize)    ' Get/Create Key
arrow.gifValue

    If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError          ' Handle Errors

    If (Asc(Mid(tmpVal, KeyValSize, 1)) = 0) Then           ' Win95 Adds Null
arrow.gifTerminated String...
        tmpVal = Left(tmpVal, KeyValSize - 1)               ' Null Found,

 
< previous page page_876 next page >

If you like this book, buy it!